package userMapper

import (
	"bookStore/config"
	"bookStore/model"
	"fmt"
	"log"
)

func FindUserByNameAndPassword(name, password string) model.User {
	var user model.User
	config.MySql.Where(model.User{Name: name, Password: password}).First(&user)
	return user
}

func AddUser(user *model.User) error {
	return config.MySql.Create(user).Error
}

func BatchAddUser(users []model.User) error {
	size := len(users)
	sql := "insert into `user_gorm` (`name`, `password`, `age`, `gender`, `phone`, `deleted`) values"
	for index, user := range users {
		if index == size-1 {
			sql += fmt.Sprintf("('%s', '%s', '%d', '%d', '%s', '%d');", user.Name, user.Password, user.Age, user.Gender,
				user.Phone, user.Deleted)
		} else {
			sql += fmt.Sprintf("('%s', '%s', '%d', '%d', '%s', '%d'),", user.Name, user.Password, user.Age, user.Gender,
				user.Phone, user.Deleted)
		}
	}
	log.Println("==========>>>user batch add sql:", sql)
	return config.MySql.Exec(sql).Error
}

func FindUserById(userId int64) model.User {
	var user model.User
	config.MySql.Where(model.User{ID: userId}).First(&user)
	return user
}
